We use the sklearn image dataset to demonstrate basic operations using Open CV.

Smoothing Images

2D Convolution (Image Filtering)

There are low-pass filters (LPF) and high-pass filters (HPF) that can help denoising one-dimensional signals. Typically, a LPF can remove noise by blurring the image whereas a HPF can be helpful in finding edges in an image.

In OpenCV, filter2D is a filtering function that replies on convolving a kernel with an image.

cv2.filter2D(src, ddepth, kernel[, dst[, anchor[, delta[, borderType]]]])  dst

Parameters:

To demonstrate this, we can consider the following example.

Example:

Image Blurring (Image Smoothing)

Image blurring is done by removing high-frequency contents (e.g: noise, edges). There are several ways to apply this.

Blur

Box blur (also known as a box linear filter) is a form of low-pass ("blurring") filter. In a box blur, each pixel is calculated using the average value of its neighboring pixels in the input image.

Moroever, Blur is a OpenCV function that blurs an image using the normalized box filter

cv2.blur(src, ksize[, dst[, anchor[, borderType]]])  dst

Parameters:

The kernel function here:

$$\texttt{K} = \frac{1}{\texttt{ksize.width*ksize.height}} \begin{bmatrix} 1 & 1 & 1 & \cdots & 1 & 1 \\ 1 & 1 & 1 & \cdots & 1 & 1 \\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\ 1 & 1 & 1 & \cdots & 1 & 1 \\ \end{bmatrix}$$

Example:

To see this filter better, we can consider using an image with some text phrases.

Example:

Box Filter

Box Filter is a OpenCV function that blurs an image using the box filter.

cv2.boxFilter(src, ddepth, ksize[, dst[, anchor[, normalize[, borderType]]]])  dst

Parameters:

The function smoothes an image using the kernel:

$$\texttt{K} = \alpha \begin{bmatrix} 1 & 1 & 1 & \cdots & 1 & 1 \\ 1 & 1 & 1 & \cdots & 1 & 1 \\ \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\ 1 & 1 & 1 & \cdots & 1 & 1 \\ \end{bmatrix}$$

where

$$\alpha = \begin{cases} \frac{1}{\texttt{ksize.width*ksize.height}}, & \mbox{when normalize=true},\\ 1, & \mbox{Otherwise}. \end{cases}$$

Example:

Gaussian Blur

Gaussian function in one dimension can be defined as $$G(x)={\frac {1}{\sqrt {2\pi \sigma ^{2}}}}e^{-{\frac {x^{2}}{2\sigma ^{2}}}}$$ In two dimensions, it is the product of two such Gaussian functions, one in each dimension: $$G(x,y)={\frac {1}{2\pi \sigma ^{2}}}e^{-{\frac {x^{2}+y^{2}}{2\sigma ^{2}}}}$$ where x is the distance from the origin in the horizontal axis, y is the distance from the origin in the vertical axis, and σ is the standard deviation of the Gaussian distribution.

In OpenCV, Gaussian Blur is defined as follows.

cv2.GaussianBlur(src, ksize, sigmaX[, dst[, sigmaY[, borderType]]])  dst

Parameters:

Example:

Median Blur

The median filter is often used to remove noise from an image that preserves edges while removing noise. See A Fast Two-Dimensional Median Filtering Algorithm by Thomas S. Huang et al. for more details regarding this filter.

Median Blur is a OpenCV function that blurs an image using the median filter.

cv2.medianBlur(src, ksize[, dst])  dst

Parameters:

Example:

Bilateral Filtering

A bilateral filter is a non-linear, edge-preserving, and noise-reducing smoothing filter for images. The bilateral filter is defined as $$I^{\text{filtered}}(x)={\frac {1}{W_{p}}}\sum _{x_{i}\in \Omega }I(x_{i})f_{r}(\|I(x_{i})-I(x)\|)g_{s}(\|x_{i}-x\|).$$

where

See Bilateral Filtering for Gray and Color Images for more details about Bilateral Filtering.

In OpenCV, Bilateral Filtering is a function that applies the bilateral filter to an image.

cv2.bilateralFilter(src, d, sigmaColor, sigmaSpace[, dst[, borderType]])  dst

Parameters:

Example:

Moreover, for adaptive bilateral filter, see this article.


Refrences

  1. OpenCV documentation
  2. Smoothing Images
  3. Box Blur
  4. Gaussian blur
  5. Bilateral filter
  6. Huang, T., G. J. T. G. Y. Yang, and Greory Tang. "A fast two-dimensional median filtering algorithm." IEEE Transactions on Acoustics, Speech, and Signal Processing 27.1 (1979): 13-18.
  7. Gavaskar, Ruturaj G., and Kunal N. Chaudhury. "Fast adaptive bilateral filtering." IEEE Transactions on Image Processing 28.2 (2018): 779-790.